package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.ArchiveBorrow;
import com.amsp.oa.oademo.mapper.ArchiveBorrowMapper;
import com.amsp.oa.oademo.service.IArchiveBorrowService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class ArchiveBorrowServiceImpl extends ServiceImpl<ArchiveBorrowMapper, ArchiveBorrow>
        implements IArchiveBorrowService {

    @Override
    public IPage<ArchiveBorrow> getPage(Integer page, Integer size, String applicantName,
            String applicantEmployeeNumber) {
        LambdaQueryWrapper<ArchiveBorrow> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.isNotBlank(applicantName)) {
            queryWrapper.like(ArchiveBorrow::getApplicantName, applicantName)
                    .or()
                    .like(ArchiveBorrow::getApplicantName2, applicantName)
                    .or()
                    .like(ArchiveBorrow::getApplicantName3, applicantName);
        }

        if (StringUtils.isNotBlank(applicantEmployeeNumber)) {
            queryWrapper.and(wrapper -> wrapper
                    .like(ArchiveBorrow::getApplicantEmployeeNumber, applicantEmployeeNumber)
                    .or()
                    .like(ArchiveBorrow::getApplicantEmployeeNumber2, applicantEmployeeNumber)
                    .or()
                    .like(ArchiveBorrow::getApplicantEmployeeNumber3, applicantEmployeeNumber));
        }

        // 按创建时间倒序排序
        queryWrapper.orderByDesc(ArchiveBorrow::getCreateTime);

        // 创建分页对象
        Page<ArchiveBorrow> pageParam = new Page<>(page, size);

        // 执行分页查询
        return this.page(pageParam, queryWrapper);
    }
}